package com.shzu.jwc.ty.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.UserStudentInfo;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.service.StudentInfoservice;

@Controller
@RequestMapping("/jwc/studentinfo")
public class JwcStudentInfocontroller {

	@Autowired
	StudentInfoservice studentInfoservice;
	
	@RequestMapping("/student.action")
	public String showstudent(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model)
	{
		PageHelper.startPage(pn, 20);
		List<UserStudentInfo> data=studentInfoservice.getstudent();
		PageInfo<UserStudentInfo> pageinfo=new PageInfo<UserStudentInfo>(data, 20);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("pageinfo", pageinfo);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("major", studentInfoservice.getmajor());
		return "jwc/studentinfo/student";
	}
	
	@RequestMapping("/selectstdent.action")
	public String selectstdent(@RequestParam("userStudentInfoYear") Integer userStudentInfoYear,@RequestParam("userStudentInfoCollege") String userStudentInfoCollege,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model,HttpSession session)
	{
		if(userStudentInfoYear==0&&userStudentInfoCollege.equals("0")) {
			PageHelper.startPage(pn, 20);
			List<UserStudentInfo> data=studentInfoservice.getstudent();
			PageInfo<UserStudentInfo> pageinfo=new PageInfo<UserStudentInfo>(data, 20);
			model.addAttribute("pageinfo", pageinfo);
		}else
		{
			PageHelper.startPage(pn, 20);
			List<UserStudentInfo> data=studentInfoservice.selectstudent(userStudentInfoYear, userStudentInfoCollege);
			PageInfo<UserStudentInfo> pageinfo=new PageInfo<UserStudentInfo>(data, 20);
			model.addAttribute("pageinfo", pageinfo);
		}
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("major", studentInfoservice.getmajor());
		session.setAttribute("userStudentInfoYear", userStudentInfoYear);
		session.setAttribute("userStudentInfoCollege", userStudentInfoCollege);
		return "jwc/studentinfo/student";
	}
	
	@RequestMapping("/searchstudent.action")
	public String searchstudent(@RequestParam("userStudentInfoNum")String userStudentInfoNum,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model)
	{
		PageHelper.startPage(pn, 20);
		List<UserStudentInfo> data=studentInfoservice.searchstudent(userStudentInfoNum);
		PageInfo<UserStudentInfo> pageinfo=new PageInfo<UserStudentInfo>(data, 20);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("pageinfo", pageinfo);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("major", studentInfoservice.getmajor());
		return "jwc/studentinfo/student";
	}
	
	@ResponseBody
	@RequestMapping(value="/upload.action",method=RequestMethod.POST)
	public Msg upload(@RequestParam("file") MultipartFile file) {
		  if(file.isEmpty())
			  return Msg.error("文件不能为空");
		  
		  String name=file.getOriginalFilename();
		  if(!name.endsWith(".xlsx")) {
			  return Msg.error("文件类型必须为xlsx");
		  }
		  try {
		  studentInfoservice.batchImport(name, file);
		  return Msg.success("导入成功");
		  }catch(Exception ex) {
			  return Msg.error("导入失败,"+ex.getMessage());
		  }
	}
	
	@ResponseBody
	@RequestMapping("/deletestudent.action")
	public Msg deletestudent(@RequestParam("id") Integer id)
	{
		try {
			studentInfoservice.delete(id);
			return Msg.success("删除成功");
		}catch(Exception ex) {
			return Msg.error("删除失败,"+ex.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/insert.action")
	public Msg insert(UserStudentInfo userStudentInfo) {
		try {
			if(studentInfoservice.isexits(userStudentInfo.getUserStudentInfoNum())>1)
				return Msg.error("该老师已存在");
			studentInfoservice.insert(userStudentInfo);
			return Msg.success("添加成功");
		}catch(Exception ex) {
			return Msg.error("添加失败,"+ex.getMessage());
		}
	}
}
